<template>
  <div class="detail">
    <detail-nav-bar @itemClick="itemClick" ref="nav" />
    <scroll
      class="wrapper"
      ref="scroll"
      :probe-type="3"
      @scroll="contentScroll"
    >
      <detail-swiper ref="detailSwiper" :top-images="topImages" />
      <detail-base-info :goods="goods" />
      <detail-shop-info :shop="shop" />
      <detail-goods-info :detail-info="detailInfo" />
      <detail-param-info ref="param" :param-info="paramInfo" />
      <detail-comment-info ref="comment" :commentInfo="commentInfo" />
      <detail-recommend-list ref="recommend" :goods="recommendList" />
    </scroll>

    <back-top @click.native="backTopClick" v-show="isShow" class="back-top" />
    <detail-bottom-bar @addToCart="addToCart" />
  </div>
</template>

<script>
import DetailNavBar from "./childComps/DetailNavBar.vue";
import DetailSwiper from "./childComps/DetailSwiper.vue";
import DetailBaseInfo from "./childComps/DetailBaseInfo.vue";
import DetailShopInfo from "./childComps/DetailShopInfo.vue";
import DetailGoodsInfo from "./childComps/DetailGoodsInfo.vue";
import DetailParamInfo from "./childComps/DetailParamInfo.vue";
import DetailCommentInfo from "./childComps/DetailCommentInfo.vue";
import DetailRecommendList from "./childComps/DetailRecommendList.vue";
import DetailBottomBar from "./childComps/DetailBottomBar.vue";
import BackTop from "components/content/backTop/BackTop.vue";

import Scroll from "components/common/scroll/Scroll.vue";

import {
  getDetail,
  getRecommend,
  Goods,
  Shop,
  GoodsParam,
} from "network/detail.js";

export default {
  name: "Detail",
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    DetailRecommendList,
    DetailBottomBar,
    BackTop,

    Scroll,
  },
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommendList: [],
      themeTopY: [],
      currentIndex: 0,
      isShow: false,
    };
  },
  created() {},
  activated() {
    // 1.保存传入的iid
    this.iid = this.$route.params.iid;
    console.log(this.iid);

    // 2.根据iid请求详情数据
    getDetail(this.iid).then((res) => {
      const data = res.result;
      this.topImages = data.itemInfo.topImages;

      // 3.获取商品信息
      this.goods = new Goods(
        data.itemInfo,
        data.columns,
        data.shopInfo.services
      );

      // 4.获取店铺信息
      this.shop = new Shop(data.shopInfo);

      // 5.获取详情图片信息
      this.detailInfo = data.detailInfo;

      // 6.获取参数的信息
      this.paramInfo = new GoodsParam(
        data.itemParams.info,
        data.itemParams.rule
      );

      // 7.获取商品的评论信息,先判断再取值
      if (data.rate.cRate !== 0) {
        this.commentInfo = data.rate.list[0];
      }
    });
    // 8.获取商品的推荐数据
    getRecommend().then((res) => {
      this.recommendList = res.data.list;
    });
  },
  updated() {
    this.$nextTick(() => {
      this.themeTopY = [];
      this.themeTopY.push(this.$refs.detailSwiper.$el.offsetTop);
      this.themeTopY.push(this.$refs.param.$el.offsetTop);
      this.themeTopY.push(this.$refs.comment.$el.offsetTop);
      this.themeTopY.push(this.$refs.recommend.$el.offsetTop);
    });
  },
  mounted() {},
  methods: {
    //点击顶部标题栏跳转到相关内容
    itemClick(index) {
      switch (index) {
        case 0:
          this.$refs.scroll.scroll.scrollToElement(
            this.$refs.detailSwiper.$el,
            500
          );
          break;

        case 1:
          this.$refs.scroll.scroll.scrollToElement(this.$refs.param.$el, 500);
          break;

        case 2:
          this.$refs.scroll.scroll.scrollToElement(this.$refs.comment.$el, 500);
          break;

        case 3:
          this.$refs.scroll.scroll.scrollToElement(
            this.$refs.recommend.$el,
            500
          );
          break;
      }
    },
    //点击当滚动到相关内容时，顶部标题栏相应做出改变
    contentScroll(position) {
      // console.log(-position.y);
      const positionY = -position.y + 44;
      if (positionY >= this.themeTopY[3]) {
        this.currentIndex = 3;
      } else if (
        positionY >= this.themeTopY[2] &&
        positionY < this.themeTopY[3]
      ) {
        this.currentIndex = 2;
      } else if (
        positionY >= this.themeTopY[1] &&
        positionY < this.themeTopY[2]
      ) {
        this.currentIndex = 1;
      } else {
        this.currentIndex = 0;
      }

      this.$refs.nav.currentIndex = this.currentIndex;

      if (-position.y > 419) {
        this.isShow = true;
      } else {
        this.isShow = false;
      }
    },
    // 返回顶部
    backTopClick() {
      this.$refs.scroll.scrollTo(0, 0, 500); //scrollTo是一个封装的方法，面向组件
    },
    // 加入购物车事件
    addToCart() {
      // 1.获取购物车需要展示的信息
      const product = {};
      product.image = this.topImages[0];
      product.title = this.goods.title;
      product.desc = this.goods.desc;
      product.price = this.goods.realPrice;
      product.iid = this.iid;

      // 2.将商品加入购物车中
      // this.$store.commit('addToCart',product);
      this.$store.dispatch('addToCart',product);
    },
  },
};
</script>
<style scoped>
.wrapper {
  height: calc(100vh - 44px - 58px);
  /* height: 500px; */
  overflow: hidden;
}

.back-top {
  position: fixed;
  right: -35px;
  bottom: 59px;
}
</style>
